<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>新增商品</title>
    <link rel="stylesheet" href="plugins/layui/css/layui.css"/>
    <!-- <link rel="stylesheet" href="css/comments.css"> -->
    <link rel="stylesheet" href="css/goods.css">
    <style>
		.maincontainer {
		    padding: 10px;
		}
        .layui-form-label{
            width: 150px;
        }
        .layui-input-block{
            margin-left: 180px;
        }
        .addFile{
            width: 100px;
            height: 30px;
            border-radius: 4px;
            border: 1px dashed #e6e6e6;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            color: #666;
            font-size: 12px;
			margin-top:20px;
        }
        .addFile:hover {
            border: 1px dashed #f58843;
            background: linear-gradient(316deg,#f58843 0%,#f56643 100%);;
            color: #fff;
        }
        .addFile:hover i {
            color: #fff;
        }
        .addFile i {
            font-size: 14px;
            color: #f5dab0;
            margin-right: 5px;
        }
        .fileUpLoadDiv{
            padding: 0 0 5px 0;
            display: none;
        }
        .fileUpLoadName{
            color: #03A9F4;
            cursor: pointer;
        }
		.fileUpLoadView{
		    cursor: pointer;
		    margin-left:10px;
		    color:#9F9F9F;
		}
		.fileUpLoadView:hover{
			color:#f56643;
		}
        .orange{
            color: #fbaf7e;
        }
		.redtext {
		    color: red !important;
		}
		.marginRight{
		    margin-right: 8px;
		}
		.productBtn{
			position: fixed;
			bottom: 0;
			left: 0;
			z-index: 99999;
			height: 40px;
			width: 100%;
			background: #f2f2f2;
			margin: 0;
			padding: 10px 195px;
		}
    </style>
</head>
<style>
</style>
<body>
<div class="maincontainer">
    <blockquote class="layui-elem-quote">
        <span class="block_title"></span>
        <button type="button" class="layui-btn layui-btn-sm" style="float: right;margin-top: -4px;" onclick="onback()">返回</button>
    </blockquote>
    <div class="all-info">
		<form class="layui-form" action="">
			<div class="layui-form-item">
			  <label class="layui-form-label namelabel"><span class="redtext marginRight">*</span>商品名称：</label>
			  <div class="layui-input-block">
				 <input type="text" class="layui-input" name="name" placeholder="请输入商品名称"  maxlength="50"/>
			  </div>
			</div>
			<div class="layui-form-item">
			  <label class="layui-form-label brandlabel">品牌：</label>
			  <div class="layui-input-inline">
                <select name="brand" lay-filter="brand" lay-search>
                  <option value=""></option>
                </select>
              </div>
			</div>
			<div class="layui-form-item layui-inline">
				<label class="layui-form-label categoryIdlabel"><span class="redtext marginRight">*</span>所属分类：</label>
				<div class="layui-input-inline">
				  <select name="parentId" lay-filter="parentId" lay-search>
					<option value="">一级分类</option>
				  </select>
				</div>
				<div class="layui-input-inline">
				  <select name="categoryId" lay-search>
					<option value="">二级分类</option>
				  </select>
				</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label unitlabel"><span class="redtext marginRight">*</span>计量单位：</label>
                <div class="layui-input-inline">
                    <select name="unit" lay-filter="unit">
                        <option value=""></option>
                        <!-- <option value="件">件</option>
                        <option value="台">台</option>
                        <option value="箱">箱</option>
                        <option value="把">把</option>
                        <option value="支">支</option>
                        <option value="次">次</option>
                        <option value="顶">顶</option>
                        <option value="条">条</option>
                        <option value="扎">扎</option>
                        <option value="百个">百个</option>
                        <option value="片">片</option>
                        <option value="斤">斤</option>
                        <option value="kg">kg</option>
                        <option value="套">套</option>
                        <option value="包">包</option>
                        <option value="桶">桶</option>
                        <option value="个">个</option>
                        <option value="瓶">瓶</option>
                        <option value="只">只</option>
                        <option value="双">双</option> -->
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label searchKeyWordlabel">排序值：</label>
                <div class="layui-input-inline">
                    <input type="Number" class="layui-input" name="sortNumber" placeholder="请输入排序值"/>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label searchKeyWordlabel">搜索关键字：</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" name="searchKeyWord" placeholder="请输入搜索关键字"  maxlength="20"/>
                </div>
            </div>
            <!-- <div class="layui-form-item">
			 	<label class="layui-form-label updateStatuslabel"><span class="redtext marginRight">*</span>是否可改单：</label>
			 	<div class="layui-input-block">
                    <input type="radio" name="updateStatus" value="1" title="是" checked="">
                    <input type="radio" name="updateStatus" value="0" title="否">
			 	</div>
            </div> -->
            <div class="layui-form-item">
			 	<label class="layui-form-label publicStatuslabel"><span class="redtext marginRight">*</span>是否公示：</label>
			 	<div class="layui-input-block">
                    <input type="radio" name="publicStatus" value="1" title="是" disabled>
                    <input type="radio" name="publicStatus" value="0" title="否" checked="" disabled>
			 	</div>
				<div class="layui-form-mid layui-word-aux">（是否公示由平台决定）</div>
			 </div>
			 <div class="layui-form-item">
			 	<label class="layui-form-label quoteStatuslabel"><span class="redtext marginRight">*</span>是否快速报价商品：</label>
			 	<div class="layui-input-block">
                    <input type="radio" name="quoteStatus" value="1" title="是" checked="">
                    <input type="radio" name="quoteStatus" value="0" title="否">
			 	</div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label productSpecVoListlabel">商品规格属性：</label>
                <div class="layui-input-block">
                    <div class="addrule typeSpecification">
                        <div class="el">
                            <button type="button" class="layui-btn layui-btn-sm layui-btn-normal addSpecification"><i class="layui-icon layui-icon-addition"></i>添加规格</button>
                        <div class="myMore"></div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
			 	<div class="layui-input-block">
					<!-- <button type="button" class="layui-btn layui-btn-sm layui-btn-normal reFrashBtn"><i class="layui-icon layui-icon-refresh"></i>刷新</button> -->
					<div class="mytable"></div>
				 </div>
			</div>
			<!-- 单规格 -->
			<div class="totalPrice">
				<div class="layui-form-item">
				  <label class="layui-form-label namelabel"><span class="redtext marginRight">*</span>默认商品价格：</label>
				  <div class="layui-input-inline">
					 <input type="text" class="layui-input" name="totalPrice"" placeholder="请输入默认商品价格"  maxlength="20"/>
				  </div>
				</div>
				<!-- 食堂等级价格 -->
				<div class="other_price"></div>
			</div>
            <div class="layui-form-item">
                <label class="layui-form-label reportDueTimelabel">检测报告到期时间：</label>
                <div class="layui-input-inline">
                    <input type="text" name="reportDueTime" class="layui-input" id="reportDueTime">
                </div>
            </div>
            <div class="layui-form-item">
			 	<label class="layui-form-label mainImagelabel"><span class="redtext marginRight">*</span>商品主图：</label>
			 	<div class="layui-input-inline">
			 		<div class="imgList">
			 			<ul class="mainpicture">
			 				<li class="nullimg mainImg">
			 					<div>
			 						<i class="layui-icon layui-icon-upload"></i>
			 						<p>上传商品主图</p>
			 					</div>
			 				</li>
			 				<li class="showMImg" style="display: none">

			 				</li>
			 			</ul>
			 			<input type="file" accept="image/png,image/jpg,image/jpeg" id="mainUp" style="display: none;" name="">
			 			<label class="addImg" for="mainUp">
			 				<i class="layui-icon layui-icon-upload"></i>上传
			 			</label>
			 			<div class="layui-form-mid layui-word-aux">建议上传图片尺寸:334*320,不超过1M</div>
			 		</div>
			 	</div>
            </div>
            <div class="layui-form-item">
			 	<label class="layui-form-label smallPiclabel">商品图片：</label>
			 	<div class="layui-input-inline">
			 		<div class="imgList">
			 			<ul class="mainpicture">
			 				<li class="nullimg detailsImg">
			 					<div>
			 						<i class="layui-icon layui-icon-upload"></i>
			 						<p>上传商品图片</p>
			 					</div>
			 				</li>
			 				<li class="showDImg1" style="display: none">

			 				</li>
			 			</ul>
			 			<input type="file" accept="image/png,image/jpg,image/jpeg" id="bannerUp" multiple style="display: none;" name="">
			 			<label class="addImg" for="bannerUp">
			 				<i class="layui-icon layui-icon-upload"></i>上传
			 			</label>
			 			<div class="layui-form-mid layui-word-aux">建议上传图片尺寸:750*680,不超过3M,最多可上传20张轮播图</div>
			 		</div>
			 	</div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label contentlabel">商品描述：</label>
                <div class="layui-input-block">
                    <div id="demo1"></div>
                    <div id="demo2"></div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label annexFileslabel">商品附件：</label>
                <div class="layui-form-mid layui-word-aux">注（请上传营业执照、生产许可证、检测报告）</div>
            </div>
			<div class="layui-form-item">
			    <div class="layui-input-block">
			        <div>
						<ul class="fileList"></ul>
			        </div>
			        <div>
			            <input type="file" accept="application/pdf,image/png,image/jpg,image/jpeg" id="fileUpLoad" multiple style="display: none;" name="">
			            <label class="addFile" for="fileUpLoad">
			                <i class="layui-icon layui-icon-upload"></i>上传
			            </label>
			        </div>
			    </div>
			</div>
            <!-- <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <div class="layui-input-block">
                    <div class="fileUpLoadDiv">

                    </div>
                    <div>
                        <input type="file" accept="application/pdf" id="fileUpLoad" multiple style="display: none;" name="">
                        <label class="addFile" for="fileUpLoad">
                            <i class="layui-icon layui-icon-upload"></i>上传
                        </label>
                    </div>
                </div>
			</div> -->
			<div class="other_remarks"></div>
		</form>

        <div class="layui-form-item" style="height: 40px;">
            <div class="layui-input-block productBtn">
                <button type="button" class="layui-btn sure"></button>
                <button type="button" class="layui-btn layui-btn-primary" onclick="onback()">取消</button>
            </div>
        </div>
	</div>
</div>

<script type="text/javascript" src="plugins/layui/layui.js"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script src="js/wangEditor.min.js"></script>
<!-- <script type="text/javascript" src="//unpkg.com/wangeditor/dist/wangEditor.min.js"></script> -->
<script>
	var E = window.wangEditor;
	var editor1 = new E('#demo1');

	var productDetail; // 商品详情
	var specificationList = [];
	var specificationList1 = '';
	var prespeciGoodList = []; // 原商品规格列表
	var speciGoodList = []; // 修改前一次商品规格列表
	// var annexFiles = [] // 附件
	var goodsPrice = [],goodsPriceCopy = []
</script>
<script src="js/productLabelFormat.js"></script>
<!-- 单位列表,品牌,分类,食堂等级 查询 -->
<script src="js/product.js"></script>
<script src="js/productEditor.js"></script>
<!-- 规格 -->
<script src="js/productSpec.js"></script>
<!-- 图片 -->
<script src="js/productImg.js"></script>
<!-- 附件 -->
<script src="js/productFile.js"></script>
<script>
	layui.config({
	    base: 'plugins/layui/modules/'
	}).use(['layer', 'form', 'http','laydate','table'], function () {
	    var layer = layui.layer,
	        http = layui.http,
	        form = layui.form,
			laydate = layui.laydate;

		laydate.render({
			elem: '#reportDueTime'
			,theme: '#fbaf7e'
		})

		window.onback = function() {
			// $(window.parent.document).find('.maincontainer').show()
			// $(window.parent.document).find('#my_iframe').hide()
			// window.parent.layui.table.reload('selectTable')
			window.history.go(-1)
		}
		var id = http.getPid('id');

		// --------------- 保存 -----------------
		// 确定 提交商品的数据
		$(".sure").click(function () {
			var name = $('input[name=name]').val();//商品名称
			var brand = $('select[name=brand]').val();//品牌
			var parentId = $('select[name=parentId]').val();//一级商品分类
			var categoryId = $('select[name=categoryId]').val();//商品分类
			var unit = $('select[name=unit]').val();//计量单位
			var sortNumber = $('input[name=sortNumber]').val() // 排序值
			var searchKeyWord = $('input[name=searchKeyWord]').val();//搜索关键字
			// var updateStatus = $('input[name=updateStatus]:checked').val();//是否可改单
			var publicStatus = $('input[name=publicStatus]:checked').val();//是否公示
			var quoteStatus = $('input[name=quoteStatus]:checked').val();//是否快速报价商品
			var reportDueTime = $('input[name=reportDueTime]').val();//检测报告到期时间
			var content = editor1.txt.html();//商品详情

			if ($.trim(name) == "") {
				layer.msg('商品名称不能为空')
				$("input[name='name']").focus();
				return false;
			}

			// if ($.trim(brand) == "") {
			// 	layer.msg('请选择品牌')
			// 	return false;
			// }
			if ($.trim(parentId) == "" && $.trim(categoryId) == "") {
				layer.msg('请选择商品分类')
				return false;
			}
			if ($.trim(unit) == "") {
				layer.msg('请选择计量单位')
				return false;
			}

			// if ($.trim(reportDueTime) == "") {
			// 	layer.msg('请选择检测报告到期时间')
			// 	$("input[name='reportDueTime']").focus();
			// 	return false;
			// }


			var productState = true;
			var productSpecParamsList = [];
			if(specificationList.length==0){ // 单规格
				if(!$('input[name=totalPrice]').val()){
					layer.msg('请输入默认商品价格');
					productState = false;
				}else{
					var list= {
						specName: '单规格',
						specValueList: [{
							value: '单规格'
						}]
					}
					productSpecParamsList.push(list)

				}

			}else{ // 多规格
				for(var x=0;x<specificationList.length;x++){
					if(!specificationList[x].name || specificationList[x].name==''){
						layer.msg('规格名称不能为空');
						productState = false;
						break;
					}
					var list= { specName:specificationList[x].name }
					var specValueList = [];                       //规格值
					for(var y=0;y<specificationList[x].p_enums.length;y++){
						var val = specificationList[x].p_enums[y];
						if(!val || val==''){
							layer.msg('规格值不能为空');
							productState = false;
							break;
						}
						var val_list = {
							value:val
						}
						specValueList.push(val_list);
					}
					list.specValueList = specValueList;
					productSpecParamsList.push(list);
				}

			}
			if(!productState){
				return
			}

			if(!content||content == '<p><br></p>'){//商品详情
				// layer.msg('请编辑商品描述');
				// return;
				content = ''
			}


			var goodState = true;
			var goodList = [];                                 // 规格明细
			goodsPriceCopy = [] // 当前是否是请求报价接口
			if(specificationList.length==0){ // 单规格
				var pp = {
					"goodNo": prespeciGoodList.length > 0?prespeciGoodList[0].goodNo:'',
					"goodImage": '',
					"price": Number($('input[name=totalPrice]').val()),
					"stockCount": 99999,
					"goodSpecParamsList":[{
						specName: '单规格',
						specValue: '单规格'
					}],                    // 商品的规格
					"goodLabelPriceList": [] // 食堂等级价格(2020/12/5新增)
				};
				goodsPriceCopy.push({
					price: Number($('input[name=totalPrice]').val())
				})

				// 食堂等级价格(2020/12/5新增)
				for(var g=0;g<canteenLabelList.length;g++){
					// 食堂等级价格必填
					if (!$('.other_price input[name=label'+canteenLabelList[g].id+']').val() || $('.other_price input[name=label'+canteenLabelList[g].id+']').val() == '') {
						layer.msg(canteenLabelList[g].labelName+'单价不能为空');
						goodState = false;
						return;
					}
					pp.goodLabelPriceList.push({
						labelName: canteenLabelList[g].id,
						goodPrice: $('.other_price input[name=label'+canteenLabelList[g].id+']').val()
					})
					goodsPriceCopy[0][canteenLabelList[g].id] = Number($('.other_price input[name=label'+canteenLabelList[g].id+']').val())
				}
				goodList.push(pp)

			}else{ // 多规格
				for(var x=0;x<specificationList1[0].length;x++){

					var pp = {
						"goodNo": $('.my_goodNo').eq(x).html(),
						"goodImage": '',
						"price":Number($('td input[name=price]').eq(x).val()),
						"stockCount": 99999,
						"goodSpecParamsList":[],                    // 商品的规格
						"goodLabelPriceList": [] // 食堂等级价格(2020/12/5新增)
					};

					if(!$('td input[name=price]').eq(x).val() || $('td input[name=price]').eq(x).val()==''){
						layer.msg('默认商品价格不能为空');
						goodState = false;
						break;
					}
					goodsPriceCopy.push({
						price: Number($('td input[name=price]').eq(x).val())
					})

					// 食堂等级价格(2020/12/5新增)
					for(var g=0;g<canteenLabelList.length;g++){
						// 食堂等级价格必填
						if (!$('td input[name=label'+canteenLabelList[g].id+']').eq(x).val() || $('td input[name=label'+canteenLabelList[g].id+']').eq(x).val() == '') {
							layer.msg(canteenLabelList[g].labelName+'单价不能为空');
							goodState = false;
							return;
						}
						pp.goodLabelPriceList.push({
							labelName: canteenLabelList[g].id,
							goodPrice: $('td input[name=label'+canteenLabelList[g].id+']').eq(x).val()
						})
						goodsPriceCopy[x][canteenLabelList[g].id] = Number($('td input[name=label'+canteenLabelList[g].id+']').eq(x).val())
					}

					for(var y=0;y<specificationList1.length;y++){
						var e = {
							specName: specificationList[y].name,
							specValue: specificationList1[y][x]
						}
						pp.goodSpecParamsList.push(e);
					}
					goodList.push(pp)
				}
			}
			if(!goodState){
				return
			}

			// 商品主图
			var mainImage
			$(".showMImg").find("img").each(function(){
				var src =  $(this).attr("onurl");
				mainImage = src
			});
			if(!mainImage){
				layer.msg('请上传商品主图');
				return;
			}

			// 商品图片
			var smallPic = [];
			$(".showDImg1").find("img").each(function(){
				var src =  $(this).attr("onurl");
				smallPic.push(src);
			});
			// if(smallPic.length==0){
				// layer.msg('请上传商品图片');
				// return;
			// }
			smallPic = JSON.stringify(smallPic);

			// 附件
			var annexFiles = [];
			$(".fileList").find("li").each(function(){
				var list = {
					filename:$(this).find("span").eq(0).html(),
					file:$(this).find("span").eq(0)[0].dataset.url
				};
				annexFiles.push(list);
			});

			// if(annexFiles.length == 0){
			// 	layer.msg('请上传商品附件');
			// 	return;
			// }

			var param = {
				name:name,// 名称
				brand:brand,// 商品品牌
				categoryId:categoryId?categoryId:parentId,//所属分类
				unit:unit,// 单位
				sortNumber: sortNumber, // 排序值
				searchKeyWord:searchKeyWord, // 搜索关键字
				// updateStatus:updateStatus,// 是否可改单
				publicStatus:publicStatus, // 是否公示
				quoteStatus: quoteStatus, // 是否快速报价商品
				productSpecParamsList:productSpecParamsList, //规格与规格值
				goodList:goodList, // 商品列表
				reportDueTime: reportDueTime, // 检测报告到期时间
				mainImage: mainImage, // 商品主图
				smallPic:smallPic, // 商品图片
				content:content, // 商品详情
				annexFiles: JSON.stringify(annexFiles), // 附件
				shelfStatus: 0,
			}
			if(id){
				param.id = id
				// 修改
				http.postJSON('supplier/admin/product/update',JSON.stringify(param),function(res){
					if(res.code == 200){
						layer.msg('操作成功')
						setTimeout(function(){
							// window.history.back()
							onback()
						},500)
					}else{
						layer.msg(res.msg)
					}
				})
			}else{
				// 添加
				http.postJSON('supplier/admin/product/add',JSON.stringify(param),function(res){
					if(res.code == 200){
						layer.msg('操作成功')
						setTimeout(function(){
							// window.history.back()
							onback()
						},500)
					}else{
						layer.msg(res.msg)
					}
				})
			}

		});


    })

</script>
</body>

</html>
